package com.ruoyi.edh.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.NumberingGetUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.edh.domain.EdhOrder;
import com.ruoyi.edh.domain.dto.EdhReturnGoodsDto;
import com.ruoyi.edh.domain.model.EdhReturnGoodsModel;
import com.ruoyi.edh.domain.vo.EdhDetailsReturnGoodsVo;
import com.ruoyi.edh.domain.vo.EdhReturnGoodsVo;
import com.ruoyi.edh.mapper.EdhOrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.edh.mapper.EdhReturnGoodsMapper;
import com.ruoyi.edh.domain.EdhReturnGoods;
import com.ruoyi.edh.service.IEdhReturnGoodsService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 易订购-退回退货Service业务层处理
 *
 * @author xxx
 * @date 2020-09-03
 */
@Service
public class EdhReturnGoodsServiceImpl implements IEdhReturnGoodsService {
    @Autowired
    private EdhReturnGoodsMapper edhReturnGoodsMapper;
    @Autowired
    private EdhOrderMapper edhOrderMapper;

    /**
     * 查询易订购-退回退货
     *
     * @param id 易订购-退回退货ID
     * @return 易订购-退回退货
     */
    @Override
    public Map<String, Object> selectEdhReturnGoodsById(Long id) {
        Map<String, Object> result = new HashMap<String, Object>();
        // 查询退货商品列表
        List<Map<String, Object>> returnGoods = edhReturnGoodsMapper.selectReturnGoodsByRgid(id);
        // 退货单详情
        EdhDetailsReturnGoodsVo detailsReturnGoodsVo = edhReturnGoodsMapper.selectEdhReturnGoodsDetailById(id);
        result.put("returnGoods", returnGoods);
        result.put("detailsReturnGoods", detailsReturnGoodsVo);
        return result;
    }

    /**
     * 查询易订购-退回退货列表
     *
     * @param edhReturnGoods 易订购-退回退货
     * @return 易订购-退回退货
     */
    @Override
    public List<EdhReturnGoods> selectEdhReturnGoodsList(EdhReturnGoods edhReturnGoods) {
        return edhReturnGoodsMapper.selectEdhReturnGoodsList(edhReturnGoods);
    }

    /**
     * 查询易订购-退回退货列表
     *
     * @param edhReturnGoods 易订购-退回退货
     * @return 易订购-退回退货
     */
    @Override
    public List<EdhReturnGoodsVo> selectEdhReturnGoodsVoList(EdhReturnGoodsModel.SearchModel edhReturnGoods) {
        return edhReturnGoodsMapper.selectEdhReturnGoodsVoList(edhReturnGoods);
    }

    /**
     * 新增易订购-退回退货
     *
     * @param edhReturnGoodsDto 易订购-退回退货
     * @return 结果
     */
    @Override
    @Transactional
    public Boolean insertEdhReturnGoods(EdhReturnGoodsDto edhReturnGoodsDto) {
        EdhReturnGoods edhReturnGoods = new EdhReturnGoods();
        edhReturnGoods.setReturnNo(NumberingGetUtils.numberingGet("th"));
        edhReturnGoods.setOrderId(edhReturnGoodsDto.getOrderId());
        edhReturnGoods.setReturnType(edhReturnGoodsDto.getReturnType());
        edhReturnGoods.setReturnReason(edhReturnGoodsDto.getReturnReason());
        edhReturnGoods.setCertificate(edhReturnGoodsDto.getCertificate());
        edhReturnGoods.setReturnNumber(edhReturnGoodsDto.getReturnNumber());
        edhReturnGoods.setUserId(SecurityUtils.getUserId().toString());
        //修改子订单信息
        EdhOrder edhOrder = new EdhOrder();
        edhOrder.setId(Long.valueOf(edhReturnGoodsDto.getOrderId()));
        edhOrder.setReturnStatus("0");
        edhOrder.setOrderStatus("5");
        int order = edhOrderMapper.updateEdhOrder(edhOrder);
        int returnGoods = edhReturnGoodsMapper.insertEdhReturnGoods(edhReturnGoods);
        return order > 0 && returnGoods > 0 ? true : false;
    }

    /**
     * @param edhReturnGoods
     * @description: 新增退货记录
     * @return: Integer
     * @author: 梁东宸
     * @time: 2020/9/17 2:41 下午
     */
    @Override
    public Integer addReturn(EdhReturnGoods edhReturnGoods) {
        // 子订单订单变成退款退货状态
        EdhOrder order = new EdhOrder();
        order.setId(edhReturnGoods.getOrderId());
        order.setOrderStatus("5");
        order.setReturnStatus("0");
        edhOrderMapper.updateOrderStatus(order);
        // 增加退货订单数据
        edhReturnGoods.setCreateBy(SecurityUtils.getUserId().toString());
        edhReturnGoods.setReturnNo(NumberingGetUtils.numberingGet("TK"));
        return edhReturnGoodsMapper.insertEdhReturnGoods(edhReturnGoods);
    }

    /**
     * 修改易订购-退回退货
     *
     * @param edhReturnGoods 易订购-退回退货
     * @return 结果
     */
    @Override
    public int updateEdhReturnGoods(EdhReturnGoods edhReturnGoods) {
        edhReturnGoods.setUpdateTime(DateUtils.getNowDate());
        return edhReturnGoodsMapper.updateEdhReturnGoods(edhReturnGoods);
    }

    /**
     * 批量删除易订购-退回退货
     *
     * @param ids 需要删除的易订购-退回退货ID
     * @return 结果
     */
    @Override
    public int deleteEdhReturnGoodsByIds(Long[] ids) {
        return edhReturnGoodsMapper.deleteEdhReturnGoodsByIds(ids);
    }

    /**
     * 删除易订购-退回退货信息
     *
     * @param id 易订购-退回退货ID
     * @return 结果
     */
    @Override
    public int deleteEdhReturnGoodsById(Long id) {
        return edhReturnGoodsMapper.deleteEdhReturnGoodsById(id);
    }

    /**
     * 审核服务单
     *
     * @param id           退货单id
     * @param isAudit      1 审核通过 2 审核不通过
     * @param returnStatus 0 未收到货物 1已收到货物
     */
    @Override
    public Integer auditReturnGoods(Long id, String isAudit, String returnStatus, String reason) {
        EdhReturnGoods edhReturnGoods = edhReturnGoodsMapper.selectEdhReturnGoodsById(id);
        if (StringUtils.isNull(edhReturnGoods)) {
            throw new CustomException("该退货单不存在或者已被删除");
        }
        if (StringUtils.isNotEmpty(isAudit)) {
            if (!"0".equals(edhReturnGoods.getIsAudit())) {
                throw new CustomException("该退货单已经被审核过了,不可重复审核");
            }
            edhReturnGoods.setAuditTime(DateUtils.getNowDate());
            edhReturnGoods.setAuditUser(SecurityUtils.getUserId());
            edhReturnGoods.setAuditReason(reason);
        }
        if (StringUtils.isNotEmpty(returnStatus)) {
            if (!"0".equals(edhReturnGoods.getReturnStatus())) {
                throw new CustomException("该退货单已经被收过货了,不可重复收货");
            }
            edhReturnGoods.setReturnTime(DateUtils.getNowDate());
            edhReturnGoods.setReturnUser(SecurityUtils.getUserId());
            edhReturnGoods.setReturnRemake(reason);
        }
        edhReturnGoods.setIsAudit(isAudit);
        edhReturnGoods.setReturnStatus(returnStatus);
        edhReturnGoodsMapper.updateEdhReturnGoods(edhReturnGoods);
        // 订单状态为已完成
        EdhOrder order = new EdhOrder();
        order.setId(edhReturnGoods.getOrderId());
        order.setReturnStatus("3");
        edhOrderMapper.updateEdhOrder(order);
        return 1;
    }

    /**
     * 获取退货单各状态的数量
     */
    @Override
    public Map<String, Object> getStatusNumber() {
        return edhReturnGoodsMapper.getStatusNumber();
    }

    @Override
    public Map<String, Object> getDetail(Long orderId) {
        return edhReturnGoodsMapper.getDetail(orderId);
    }
}
